package xyc.sjxy.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import xyc.sjxy.utils.JdbcUtils;
import xyc.sjxy.utils.ReflecUtils;

import java.sql.SQLException;
import java.util.List;

public class BaseDao<T> {
    private QueryRunner queryRunner = null;
    private Class<T> type;
    public BaseDao() {
        this.type = ReflecUtils.getSuperGenericType(this.getClass());
        this.queryRunner = new QueryRunner(JdbcUtils.getDataSource());
    }
    public T selectOne(String sql, Object... args) throws SQLException {
        return this.queryRunner.query(sql, new BeanHandler<T>(type), args);
    }

    public List<T> selectList(String sql, Object ... args) throws SQLException {
        return this.queryRunner.query(sql, new BeanListHandler<T>(type), args);
    }
    public int update(String sql, Object ... args) throws SQLException {
        return this.queryRunner.update(sql, args);
    }
}
